package com.xingkeduo.controller;

import com.xingkeduo.model.param.BatchLockStockParam;
import com.xingkeduo.model.param.StockParam;
import com.xingkeduo.model.vo.*;
import com.xingkeduo.service.OtherService;
import com.xingkeduo.service.StaffAttendanceService;
import com.xingkeduo.service.StaffStockService;
import com.xingkeduo.util.TimeUtil;
import com.xingkeduo.util.tool.ResponseMessage;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Date 2019/3/29 11:13
 * @Created 情绪十分不稳定的Shadow
 */

@RestController
@RequestMapping("/staff/stock")
public class StaffStockController {

    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(StaffStockController.class);

    @Autowired
    public StaffStockService staffStockService;

    @Autowired
    public StaffAttendanceService attendanceService;

    @Autowired
    public OtherService otherService;

    /**
     * @description  获取发型师库存
     * @Author 情绪极其不稳定的Shadow
     * @Param []
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @GetMapping("/getStaffStock")
    public ResponseMessage getStaffStock(Long staffId, Long storeId, String starDate){
        if(StringUtils.isEmpty(staffId) || StringUtils.isEmpty(storeId)){
            return ResponseMessage.error("店铺ID或者发型师ID不能为空", 500);
        }
        Map<String, StockVo> page = staffStockService.getStaffStock(staffId, storeId, starDate);
        return ResponseMessage.ok(page);
    }


    /**
     * @description  获取店铺下 所有发型师总排班天数 and 发型师部分信息
     * @Author 情绪极其不稳定的Shadow
     * @Param [storeId]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @GetMapping("/getStaffSchedulingByStoreId")
    public ResponseMessage getStaffSchedulingByStoreId(Long storeId){
        if(StringUtils.isEmpty(storeId)){
            return ResponseMessage.error("店铺id不能为空", 500);
        }
        List<StaffSchedulingVo> schedulingVos = attendanceService.getStaffSchedulingByStoreId(storeId);
        return ResponseMessage.ok(schedulingVos);
    }

    /**
     * @description  根据发型师id 获取所有店铺总排班天数 and 店铺名称
     * @Author 情绪极其不稳定的Shadow
     * @Param [staffId]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @GetMapping("/getStoreSchedulingByStaffId")
    public ResponseMessage getStoreSchedulingByStaffId(Long staffId, String storeName){
        if(StringUtils.isEmpty(staffId)){
            return ResponseMessage.error("发型师id不能为空", 500);
        }
        List<StoreSchedulingVo> StoreSchedulingVos = attendanceService.getStoreSchedulingByStaffId(staffId,storeName);
        return ResponseMessage.ok(StoreSchedulingVos);
    }

    /**
     * @description 1.修改店铺营业时间 or 最大库存颗粒时 需要找到影响的订单 组装数据返回
     *              2.删除人员时 找到当天及以后受影响的订单
     * @Author 情绪极其不稳定的Shadow
     * @Param [shopId]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @RequestMapping("/stockOrder")
    public ResponseMessage stockOrder(Long shopId, @RequestBody List<Long> staffIds, String selectDate){
        logger.info("查询受影响订单接口入参");
        logger.info("店铺id："+shopId);
        logger.info("查询时间："+ selectDate);
        logger.info("发型师ids"+staffIds.toString());
        if(StringUtils.isEmpty(shopId)){
            return ResponseMessage.error("店铺Id不能为空", 500);
        }
        List<OrderInfoVo> orderInfoVos = staffStockService.stockOrder(shopId,staffIds,TimeUtil.formatDate(selectDate));
        return ResponseMessage.ok(orderInfoVos);
    }

    /**
     * @description  条件：修改店铺营业时间 or 最大库存颗粒时。
     *               1.清空店铺班次设置。
     *               2.把第二天及以后的排班时间置空。
     *               3.把当周及以后的所有早晚全班全部置为临时状态。
     * @Author 情绪极其不稳定的Shadow
     * @Param [shopId, serverTime, businessTime, modifier]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @GetMapping("/updateServerTimeAndBusinessTime")
    public ResponseMessage updateServerTimeAndBusinessTime(String shopId, String serverTime, String businessTime, String modifier){
        if(StringUtils.isEmpty(shopId)){
            return ResponseMessage.error("店铺id不能为空", 500);
        }
        if(StringUtils.isEmpty(modifier)){
            return ResponseMessage.error("操作人id不能为空", 500);
        }
        int i = staffStockService.updateServerTimeAndBusinessTime(shopId, serverTime, businessTime, modifier);
        if(i > 0){
            return ResponseMessage.ok("对应班次、排班已重置，对应库存已清空");
        }
        if(i == -1){
            return  ResponseMessage.ok("店铺id："+shopId+",在发型师端无店铺班次设置");
        }
        return ResponseMessage.error("操作失败", 500);
    }

    /**
     * @description  订单系统 锁定库存 操作
     * @Author 情绪极其不稳定的Shadow
     * @Param [stockId, orderNo, status, lineNo]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @GetMapping("/lockStockBystockId")
    public ResponseMessage lockStockBystockId(Long stockId, String orderNo, Byte status, String lineNo, String userPath, Long storeId){
        logger.info("开始调用锁定库存接口");
//        if(StringUtils.isEmpty(stockId)){
//            return ResponseMessage.error("缺少库存Id", 500);
//        }
        if(StringUtils.isEmpty(storeId)){
            return ResponseMessage.error("缺少店铺Id", 500);
        }
        if(StringUtils.isEmpty(orderNo)){
            return ResponseMessage.error("缺少订单号", 500);
        }
//        if(StringUtils.isEmpty(userPath)){
//            return ResponseMessage.error("缺少用户头像", 500);
//        }
        if(null == status){
            return ResponseMessage.error("缺少状态", 500);
        }
        int i = staffStockService.lockStockBystockId(stockId, Long.parseLong(orderNo), status, lineNo, userPath, storeId);
        if(i > 0){
            return ResponseMessage.ok("锁定库存id--->>>"+stockId+"订单状态成功");
        }else if(i == -1){
            return ResponseMessage.error("锁定库存id--->>>"+stockId+"||库存orderNo--->>>"+orderNo+"的库存已经不存在,请查看排班情况",500);
        }else if(i == -2){
            return ResponseMessage.error("锁定库存id--->>>"+stockId+"||库存orderNo--->>>"+orderNo+"的库存已经被其他用户锁定",500);
        }else if(i == -3){
            return ResponseMessage.error("锁定库存id--->>>"+stockId+"||库存orderNo--->>>"+orderNo+"的库存不是空闲状态",500);
        }
        return ResponseMessage.error("锁定库存id--->>>"+stockId+"订单状态失败", 500);
    }

    /**
     * @description  订单系统 解锁库存 操作
     * @Author 情绪极其不稳定的Shadow
     * @Param [stockId]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @GetMapping("/unlockStockBystockId")
    public ResponseMessage unlockStockBystockId(String orderNo, Long storeId){
        if(StringUtils.isEmpty(orderNo)){
            return ResponseMessage.error("缺少订单Id", 500);
        }
        if(StringUtils.isEmpty(storeId)){
            return ResponseMessage.error("缺少店铺Id", 500);
        }
        int i = staffStockService.unlockStockBystockId(storeId, Long.parseLong(orderNo));
        if(i > 0){
            return ResponseMessage.ok("释放库存订单状态成功");
        }else if(i == -1){
            return ResponseMessage.error("锁定库存orderNo--->>>"+orderNo+"的库存已经不存在,请查看排班情况",500);
        }
        return ResponseMessage.error("释放库存订单状态失败", 500);
    }

    /**
     * @description  订单系统查询当前库存
     * @Author 情绪极其不稳定的Shadow
     * @Param [staffId, storeId, startTime, selectDate]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @GetMapping("/getStockIdByStaffIdAndStoreId")
    public ResponseMessage getStockIdByStaffIdAndStoreId(Long staffId, Long storeId, String startTime, String selectDate){
        if(StringUtils.isEmpty(staffId) || StringUtils.isEmpty(storeId)){
            return ResponseMessage.error("缺少店铺Id or 缺少发型师Id");
        }
        if(null == startTime || null == selectDate){
            return ResponseMessage.error("缺少库存开始时间 or 缺少库存日期");
        }
        StockSmallProgramVo stockSmallProgramVo = staffStockService.getStockIdByStaffIdAndStoreId(staffId, storeId, startTime, selectDate);
        return ResponseMessage.ok(stockSmallProgramVo);
    }

    /**
     * @description 手动休息接口
     * @Param [id, status, managerId]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @GetMapping("/changeStockStatusRestOrOccupy")
    public ResponseMessage changeStockStatusRestOrOccupy(Long id,byte status,Long managerId){
     if(null==id){
         return ResponseMessage.error("id不可空");
     }
     int updateStatus=staffStockService.updateStockStatusRestOrOccupy(id,status,managerId);
     if(updateStatus>0){
         return ResponseMessage.ok("变更成功");
     }
     if(updateStatus==0){
         return ResponseMessage.error("未能变更，尝试刷新库存状态");
     }
     if(updateStatus==-2){
         return ResponseMessage.error("当前时间店铺工位已满，打开此库存请先让其他发型师点击休息");
     }
     if(updateStatus==-3){
         return ResponseMessage.error("未能变更，库存可能已被删除或者状态已变更,尝试刷新库存状态");
     }
     if(updateStatus==-5){
         return ResponseMessage.error("系统调用错误，未能查询到店铺相关信息，检查传参，或者联系店铺相关负责人");
     }
         return ResponseMessage.error("检测参数是否完整");
    }

    /**
     * @description  根据门店id获取所有可约发型师的id及最早可约时间
     * @Author 情绪极其不稳定的Shadow
     * @Param [storeId, selectDate]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @GetMapping("/getMinFreeStockByStoreId")
    public ResponseMessage getMinFreeStockByStoreId(Long storeId, String selectDate){
        if(StringUtils.isEmpty(storeId)){
            return ResponseMessage.error("缺少店铺Id", 500);
        }
        List<StockWorkTimeVo> vos = staffStockService.getMinFreeStockByStoreId(storeId, selectDate);
        return ResponseMessage.ok(vos);
    }

    /**
     * @description  根据店铺Id 发型师Id 订单号 库存状态 排队号 锁定最早可约库存
     * @Author 情绪极其不稳定的Shadow
     * @Param [stockParam]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @PostMapping("/lockMinFreeStock")
    public ResponseMessage lockMinFreeStock(@RequestBody StockParam stockParam){
        if(StringUtils.isEmpty(stockParam.getOrderNo())){
            return ResponseMessage.error("缺少订单号", 500);
        }
        if(null == stockParam.getStatus()){
            return ResponseMessage.error("缺少状态", 500);
        }
        if(StringUtils.isEmpty(stockParam.getStaffId())){
            return ResponseMessage.error("缺少发型师Id", 500);
        }
        if(StringUtils.isEmpty(stockParam.getStoreId())){
            return ResponseMessage.error("缺少店铺Id", 500);
        }
        StockForOrderVo stockForOrderVo = staffStockService.lockMinFreeStock(stockParam);
        return ResponseMessage.ok(stockForOrderVo);
    }

    /**
     * @description 根据店铺id 发型师id 预约时间 获取排队人数
     * @Author 情绪极其不稳定的Shadow
     * @Param [DateTime, staffId, storeId]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @GetMapping("/getLineNum")
    public ResponseMessage getLineNum(String dateTime,String staffId, String storeId){
        if(dateTime.length() != 19){
            return ResponseMessage.error("时间格式有误,请用yyyy-MM-dd HH:mm:ss",500);
        }
        if(StringUtils.isEmpty(staffId) || StringUtils.isEmpty(storeId)){
            return ResponseMessage.error("缺少发型师Id or 缺少店铺Id");
        }
        LineNumVo lineNum = staffStockService.getLineNum(dateTime, staffId, storeId);
        return ResponseMessage.ok(lineNum);
    }

    /**
     * @description  下单屏 获取 排队情况列表
     * @Author 情绪极其不稳定的Shadow
     * @Param [storeId]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @GetMapping("/getOrderScreenStock")
    public ResponseMessage getOrderScreenStock(String storeId){
        if(StringUtils.isEmpty(storeId)){
            return ResponseMessage.error("缺少店铺Id");
        }
        OrderScreenVo orderScreenStocks = staffStockService.getOrderScreenStock(storeId);
        return ResponseMessage.ok(orderScreenStocks);
    }

    /**
     * @description 发型师端修改发型师sku&价格 同步下单屏数据接口
     * @Author 情绪极其不稳定的Shadow
     * @Param []
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @GetMapping("/receiveStaffId")
    public ResponseMessage receiveStaffId(Long staffId){
        logger.info("接口receiveStaffId");
        logger.info("接口请求时间"+TimeUtil.DateToFormat(new Date(), "yyyy-MM-dd HH:mm:ss"),"发型师ID："+staffId);
        logger.info("发型师ID："+staffId);
        staffStockService.receiveStaffId(staffId);
        return ResponseMessage.ok("数据已发送");
    }


    /**
     * @description  订单系统查询当前库存 Baber业务需求 返回当前时间节点and下一个时间节点双库存Id
     * @Author 情绪极其不稳定的Shadow
     * @Param [staffId, storeId, startTime, selectDate]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @GetMapping("/getStockIdByStaffIdAndBaberStoreId")
    public ResponseMessage getStockIdByStaffIdAndBaberStoreId(Long staffId, Long storeId, String startTime, String selectDate){
        if(StringUtils.isEmpty(staffId) || StringUtils.isEmpty(storeId)){
            return ResponseMessage.error("缺少店铺Id or 缺少发型师Id");
        }
        if(null == startTime || null == selectDate){
            return ResponseMessage.error("缺少库存开始时间 or 缺少库存日期");
        }
        List<StockSmallProgramVo> stockSmallProgramVos = staffStockService.getStockIdByStaffIdAndBaberStoreId(staffId, storeId, startTime, selectDate);
        return ResponseMessage.ok(stockSmallProgramVos);
    }

    /**
     * @description  订单系统 批量锁定库存 操作
     * @Author 情绪极其不稳定的Shadow
     * @Param [stockId, orderNo, status, lineNo]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @PostMapping("/batchLockStockBystockId")
    public ResponseMessage batchLockStockBystockId(@RequestBody List<BatchLockStockParam> batchLockStockParams){
        logger.info("开始调用批量锁定库存接口");
        if(CollectionUtils.isEmpty(batchLockStockParams)){
            return ResponseMessage.error("缺少批量锁定库存参数");
        }
        for(BatchLockStockParam batchLockStockParam : batchLockStockParams){
            if(StringUtils.isEmpty(batchLockStockParam.getStoreId())){
                return ResponseMessage.error("缺少店铺Id", 500);
            }
            if(StringUtils.isEmpty(batchLockStockParam.getOrderNo())){
                return ResponseMessage.error("缺少订单号", 500);
            }
            if(null == batchLockStockParam.getStatus()){
                return ResponseMessage.error("缺少状态", 500);
            }
        }
        int i = staffStockService.batchLockStockBystockId(batchLockStockParams);
        if(i > 0){
            return ResponseMessage.ok("锁定库存id--->>>"+batchLockStockParams.get(0).getStoreId()+","+batchLockStockParams.get(1).getStoreId()+"订单状态成功");
        }else if(i == -1){
            return ResponseMessage.error("锁定库存id--->>>"+batchLockStockParams.get(0).getStoreId()+","+batchLockStockParams.get(1).getStoreId()
                    +"||库存orderNo--->>>"+batchLockStockParams.get(0).getOrderNo()+","+batchLockStockParams.get(1).getOrderNo()+"的库存已经不存在,请查看排班情况",500);
        }else if(i == -2){
            return ResponseMessage.error("锁定库存id--->>>"+batchLockStockParams.get(0).getStoreId()+","+batchLockStockParams.get(1).getStoreId()
                    +"||库存orderNo--->>>"+batchLockStockParams.get(0).getOrderNo()+","+batchLockStockParams.get(1).getOrderNo()+"的库存已经被其他用户锁定",500);
        }else if(i == -3){
            return ResponseMessage.error("锁定库存id--->>>"+batchLockStockParams.get(0).getStoreId()+","+batchLockStockParams.get(1).getStoreId()
                    +"||库存orderNo--->>>"+batchLockStockParams.get(0).getOrderNo()+","+batchLockStockParams.get(1).getOrderNo()+"的库存已经不是空闲状态，锁定失败",500);
        }
        return ResponseMessage.error("锁定库存id--->>>"+batchLockStockParams.get(0).getStoreId()+","+batchLockStockParams.get(1).getStoreId()+"订单状态失败", 500);
    }

    /**
     * @description  订单系统 批量解锁库存 操作
     * @Author 情绪极其不稳定的Shadow
     * @Param [stockId]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @PostMapping("/batchUnlockStockBystockId")
    public ResponseMessage batchUnlockStockBystockId(@RequestBody List<BatchLockStockParam> batchLockStockParams){
        logger.info("开始调用批量解锁库存接口");
        if(CollectionUtils.isEmpty(batchLockStockParams)){
            return ResponseMessage.error("缺少批量锁定库存参数");
        }
        if(batchLockStockParams.size() != 2){
            return ResponseMessage.error("批量解锁库存数量不为2,有问题");
        }
        for(BatchLockStockParam batchLockStockParam : batchLockStockParams){
            if(StringUtils.isEmpty(batchLockStockParam.getOrderNo())){
                return ResponseMessage.error("缺少订单Id", 500);
            }
            if(StringUtils.isEmpty(batchLockStockParam.getStoreId())){
                return ResponseMessage.error("缺少店铺Id", 500);
            }
        }

        int i = staffStockService.batchUnlockStockBystockId(batchLockStockParams);
        if(i > 0){
            return ResponseMessage.ok("释放库存订单状态成功");
        }else if(i == -1){
            return ResponseMessage.error("锁定库存orderNo--->>>"+batchLockStockParams.get(0).getOrderNo()+
                    batchLockStockParams.get(1).getOrderNo() + "的库存已经不存在,请查看排班情况",500);
        }
        return ResponseMessage.error("释放库存订单状态失败", 500);
    }

    /**
     * CMS查看发型师库存
     * @param storeId
     * @param date
     * @return
     */
    @GetMapping("/getShopStockInfo")
    public ResponseMessage getShopStockInfo(String date,Long storeId){
        if(StringUtils.isEmpty(storeId)){
            return ResponseMessage.error("店铺Id不存在", 500);
        }
        if(StringUtils.isEmpty(date)){
            return ResponseMessage.error("日期不能为空", 500);
        }
        CmsShopInfoVo shopStockInfo = staffStockService.getShopStockInfo(date,storeId);
        return ResponseMessage.ok(shopStockInfo);
    }

    /**
     *API查看发型师库存
     */
    @GetMapping("/getApiShopStockInfo")
    public ResponseMessage getApiShopStockInfo(String date,Long storeId){
        if(StringUtils.isEmpty(storeId)){
            return ResponseMessage.error("店铺Id不存在", 500);
        }
        if(StringUtils.isEmpty(date)){
            return ResponseMessage.error("日期不能为空", 500);
        }
        ApiShopInfoVo apiShopStockInfo = staffStockService.getApiShopStockInfo(date, storeId);
        return ResponseMessage.ok(apiShopStockInfo);
    }


    /**
     * @description  根据店铺Id 订单Id 查询库存是否存在
     * @Author 情绪极其不稳定的Shadow
     * @Param [storeId, orderNo]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @GetMapping("/getStockIsEnable")
    public ResponseMessage getStockIsEnable(Long storeId, String orderNo){
        if(StringUtils.isEmpty(storeId)){
            return ResponseMessage.error("缺少店铺Id");
        }
        if(StringUtils.isEmpty(orderNo)){
            return ResponseMessage.error("缺少订单Id");
        }
        Boolean stockIsEnable = staffStockService.getStockIsEnable(storeId, Long.parseLong(orderNo));
        return ResponseMessage.ok(stockIsEnable);
    }

    /**
     * @description  改约 解锁 锁定库存接口
     * @Author 情绪极其不稳定的Shadow  TODO 还不能用 2019-08-08
     * @Param [orderNo, storeId, stockId]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @GetMapping("/changeStockBystockIdAndorderNo")
    public ResponseMessage changeStockBystockIdAndorderNo(String orderNo, Long storeId, Long stockId, Byte status, String lineNo){
        if(StringUtils.isEmpty(stockId)){
            return ResponseMessage.error("缺少改约库存Id", 500);
        }
        if(StringUtils.isEmpty(orderNo)){
            return ResponseMessage.error("缺少订单Id", 500);
        }
        if(StringUtils.isEmpty(storeId)){
            return ResponseMessage.error("缺少店铺Id", 500);
        }
        if(null == status){
            return ResponseMessage.error("缺少改约状态", 500);
        }
        try {
	        int i = staffStockService.changeStockBystockIdAndorderNo(storeId, Long.parseLong(orderNo), stockId, status, lineNo);
	        if(i > 0){
	            return ResponseMessage.ok("释放库存订单状态成功");
	        }else if(i == -1){
	            return ResponseMessage.error("锁定库存orderNo--->>>"+orderNo+"的库存已经不存在,请查看排班情况",500);
	        }
        }catch(Exception e) {
        	 return ResponseMessage.error("释放库存订单状态失败", 500);
        }
        return ResponseMessage.error("释放库存订单状态失败", 500);
    }

//    /**
//     * @description  BaBer 改约 解锁 锁定库存接口
//     * @Author 情绪极其不稳定的Shadow
//     * @Param [batchLockStockParams]
//     * @return com.xingkeduo.util.tool.ResponseMessage
//     **/
//    @PostMapping("/BatchChangeStockBystockId")
//    public ResponseMessage BatchChangeStockBystockId(@RequestBody List<BatchLockStockParam> batchChangeStockParams){
//        logger.info("开始调用BaBer改约,解锁&锁定库存接口");
//        if(CollectionUtils.isEmpty(batchChangeStockParams)){
//            return ResponseMessage.error("缺少BaBer改约,解锁&锁定库存参数");
//        }
//        if(batchChangeStockParams.size() != 2){
//            return ResponseMessage.error("批量解锁库存数量不为2,不是Baber业务!");
//        }
//        for(BatchLockStockParam batchChangeStockParam : batchChangeStockParams){
//            if(StringUtils.isEmpty(batchChangeStockParam.getOrderNo())){
//                return ResponseMessage.error("缺少订单Id", 500);
//            }
//            if(StringUtils.isEmpty(batchChangeStockParam.getStoreId())){
//                return ResponseMessage.error("缺少店铺Id", 500);
//            }
//        }
//
//        int i = staffStockService.batchUnlockStockBystockId(batchChangeStockParams);
//        if(i > 0){
//            return ResponseMessage.ok("释放库存订单状态成功");
//        }else if(i == -1){
//            return ResponseMessage.error("锁定库存orderNo--->>>"+batchChangeStockParams.get(0).getOrderNo()+
//                    batchChangeStockParams.get(1).getOrderNo() + "的库存已经不存在,请查看排班情况",500);
//        }
//        return ResponseMessage.error("释放库存订单状态失败", 500);
//    }

}
